﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using E2_WEBSITE.Code.Comment;
using System.Collections.Generic;

public partial class Products_ProductDetail_comments : System.Web.UI.Page
{
    private CommentController m_CommentController = new CommentController();

    private string sProductId = string.Empty;
    private string sCategoryId = string.Empty;
    private int iVote;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            iVote = int.Parse(DropDownListDiem.SelectedValue);
        }
        else
        {
            sCategoryId = Request.QueryString["CategoryId"];
            sProductId = Request.QueryString["ProductId"];

            DropDownListDiem.SelectedValue = "10";
            iVote = 10;
            BindingComment();
        }
    }

    private void BindingComment()
    {
        CommentController commentController = new CommentController();
        if (sProductId != null)
        {
            List<CommentInfo> lstComment = commentController.getListCommentByIDProduct(int.Parse(sProductId));
            if (lstComment != null)
            {
                this.Repeater1.DataSource = lstComment;
                this.Repeater1.DataBind();
            }
        }
    }

    protected void btnDongY_Click(object sender, EventArgs e)
    {
        int iDaDangNhap = (Int32)Session["IsLogin"];
        if (iDaDangNhap != 1)
        {
            lMessage.ForeColor = Color.Red;
            lMessage.Text = "Bạn phải đăng nhập trước";
        }
        else
        {
            try
            {
                sProductId = Request.QueryString["ProductId"];
                int userId = (Int32)Session["UserID"];
                
                bool result = m_CommentController.insertComment(userId, int.Parse(sProductId), iVote);
                if (result)
                {
                    lMessage.ForeColor = Color.Blue;
                    lMessage.Text = "Đánh giá sản phẩm thành công";
                    BindingComment();
                }
                else
                {                    
                    lMessage.ForeColor = Color.Red;
                    lMessage.Text = "Đánh giá sản phẩm thất bại";
                }
            }
            catch (Exception ex)
            {                
                lMessage.ForeColor = Color.Red;
                lMessage.Text = ex.Message;
            }
        }    
    }
}
